<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/SessionRoster.class.php";
require_once dirname(__FILE__)."/ObjectContainer.class.php";
require_once dirname(__FILE__)."/TradeskillHeaders.class.php";
require_once dirname(__FILE__)."/TradeskillContainer.class.php";

class Craft
{
	static private $tradeskill_headers;
	static private $tradeskill_container;
	static private $session_roster;
	static private $object_container;
	
	private $profession_custom_id;
	private $category_name;	// Depending on the current locale
	private $item_id;
	private $spell_id;
	
	private $characters = array();	// By Locale
	private $reagents = array();	// Same
	private $names = array();		// Same
	
	function Craft($t)
	{
		if ( empty(self::$tradeskill_headers) )
		{
			self::$tradeskill_headers = TradeskillHeaders::GetInstance();
			self::$tradeskill_container = TradeskillContainer::GetInstance();
			self::$session_roster = SessionRoster::GetInstance();
			self::$object_container = ObjectContainer::GetInstance();
		}
		
		$this->profession_custom_id = $t['profession_custom_id'];
		$this->item_id = $t['PItemId'];
		$this->spell_id = $t['SpellId'];
		$this->custom_id = $t['profession_custom_id'];
		
		$this->characters = explode(",", $t['Characters']);
		
		sort($this->characters);
		
		$locales = explode(",", $t['Locales']);
		$names = explode("_\$_", $t['CraftNames']);
		
		while ( $locale = current($locales) )
		{
			$this->names[$locale] = current($names);
			
			next($locales);
			next($names);
		}

		$this->category_name = self::$tradeskill_container->IsEnchanting($t['ProfessionName']) ? self::$tradeskill_headers->GetEnchantHeader( $this->names[key($this->names)] ) : self::$tradeskill_headers->GetNormalHeader($t['CategoryName']);
		
		$reagents = explode(",", $t['Reagents']);
		foreach ($reagents as $reagent)
		{
			$t_reagent = explode(":", $reagent);
			$this->reagents[$t_reagent[0]] = $t_reagent[1];			
		}
	}
	
	function GetCategoryName()
	{
		return $this->category_name;
	}
	
	function GetCharacters()
	{
		return $this->characters;
	}
	
	function IsItem()
	{
		if ($this->item_id != 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function IsSpell()
	{
		if ($this->spell_id != 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function GetItemId()
	{
		return $this->item_id;
	}
	
	function GetUniqueId()
	{
		return $this->profession_custom_id;
	}
	
	function GetSpellId()
	{
		return $this->spell_id;
	}
	
	function HasNameThisLocale()
	{
		if ( array_key_exists(self::$session_roster->GetLocalization(), $this->names) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function GetNamesAll()
	{
		return $this->names;
	}
	
	function GetNameThisLocale()
	{
		if ( array_key_exists(self::$session_roster->GetLocalization(), $this->names) )
		{
			return $this->names[self::$session_roster->GetLocalization()];
		}
		
		return null;
	}
	
	function GetNamesOtherLocales()
	{
		$t_return = array();
		
		foreach ($this->names as $locale_id => $locale_name)
		{
			if ( $locale_id != self::$session_roster->GetLocalization() )
			{
				$t_return[$locale_id] = $locale_name;
			}
		}
		
		ksort($t_return);
		
		return $t_return;
	}
	
	function HasReagentsThisLocale()
	{
		return $this->HasNameThisLocale();
	}
	
	function GetReagentsOtherLocales()
	{
		$t_return = array();
		
		foreach ($this->names as $locale_id => $locale_name)
		{
			if ( $locale_id != self::$session_roster->GetLocalization() )
			{
				array_push($t_return, $locale_id);
			}
		}
		
		sort($t_return);
		
		return $t_return;
	}
	
	function GetReagents()
	{
		return $this->reagents;
	}
}

?>